<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('sp_class', LIB_PATH);

$main = new Sp($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

switch ($cf_action) {
    case "index":
	$smarty->mypage($cf_controller, $cf_action, 0);

	//echo get_low_price(0,0,5);
	//exit;	
	$stype_ary = add_array_item(array('' => '全部'), $cf_info_var_common['sp_contract_type']);
	$stype = isset($_POST['stype']) ? trim($_POST['stype']) : urldecode($_GET['stype']);
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode($_GET['skey2']);
	$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode($_GET['skey3']);
    $skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode($_GET['skey4']);
    $skey5 = isset($_POST['skey5']) ? trim($_POST['skey5']) : urldecode($_GET['skey5']);
    $skey6 = isset($_POST['skey6']) ? trim($_POST['skey6']) : urldecode($_GET['skey6']);
	$skey7 = isset($_POST['skey7']) ? trim($_POST['skey7']) : urldecode($_GET['skey7']);
	$skey8 = isset($_POST['skey8']) ? trim($_POST['skey8']) : urldecode($_GET['skey8']);
	$skey9 = isset($_POST['skey9']) ? trim($_POST['skey9']) : urldecode($_GET['skey9']);
	$order = trim($_POST['order']) ? trim($_POST['order']) : trim($_GET['order']);
	$where = "`sp_check`='0'";
	

	if ($skey) {
	    $where .= " and sp_title like '%" . trim($skey) . "%'";
	}
	if ($skey2) {
	    $where .= " and sp_name = '" . $skey2 . "'";
	}
	if ($skey3) {
	    $where .= " and sp_contact = '" . $skey3 . "'";
	}
	if ($skey4) {
	    $where .= " and sp_phone = '" . $skey2 . "'";
	}
	
	if ($skey5) {
		if($skey6){
			$where .= " and sp_reg_time between '" . $skey5 . "' and '".$skey6."'";
		}else{
			$where .= " and sp_reg_time >= '" . $skey5 . "'";
		}
        
    }
	if (is_numeric($self)) {
		$where .= " and sp_status = '" . $self . "'";
	}
	switch($order){
			case "money":
				$key_order = "sp_money desc";
			break;	
			default:
				$key_order ="sp_id DESC";	
			break;
		}
	
	$key_word= '?dpt=' . $cf_dpt . '&self=' . $self. '&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3. '&skey4=' . $skey4 . '&skey5=' . $skey5 . '&skey6=' . $skey6 . '&stype=' . $stype ;
	
	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, $key_order, intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page.$key_word.'&order='.$order );
	//$MyPage->get_sql();
	if($data_ary){
		foreach($data_ary as $k=>$v){
			$data_ary[$k]['url'] = SITE_ADDR.'default/index.htm?px='.$v['sp_name'];	
		}
	}
	break;
    case "add":
	if ($cf_ispost) {
		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['sp_reg_time'] = date("Y-m-d H:i:s");
		if($data['sp_name'] && !$data['sp_pwd'])
		my_jump('登录名与密码不能为空');
		$data['sp_pwd'] = base64_encode(md5($data['sp_pwd']));

	    //$db->debug(1,1);
	    $result = $main->insert_data($data);

	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
		my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
		my_jump('添加失敗');
	    }
	} else {
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
	    //dump($_REQUEST);
	}

	break;
    case "edit":
	if ($cf_ispost) {

		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['sp_edit_time'] = date("Y-m-d H:i:s");
		if($data['sp_pwd']){
			$data['sp_pwd'] = base64_encode(md5($data['sp_pwd']));
		}else{
			unset($data['sp_pwd']);	
		}
 
 		$row = $main->get_data_row($id,"sp_agent_uid");
		if($row['sp_agent_uid']!=$data['sp_agent_uid']){
			$data['sp_agent_update_info'] = $cf_admin_info['admin_name'].' '.date("Y-m-d H:i:s");
		}
 		
	    //$db->debug(1,1);
	    $result = $main->update_data($data, "sp_id='$id'");

	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		my_success('修改成功', '', $return_page, 20);
	    } else {
		my_error('修改失败');
	    }
	} else {
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $row = $main->get_data_row($id);
	    if (!$row)
		my_jump('沒有此信息');

	    //my_print($row['item']);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
	}
	break;
    case "del":
	$id = intval($_GET['id']);
	$result = $main->del_sp($id);

	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失敗!');
	}

	break;
    case "check":
	$value = intval($_GET['value']);
	$reason = trim($_GET['value2']);
	$where = "sp_id = '" . $id . "'";
	$data = array(
	    'sp_check' => intval($value),
	    'sp_check_info' => $cf_admin_info['admin_name'].' '.date("Y-m-d H:i:s"),
	    'sp_fail_reason' => $reason,
	);
	//$db->debug(1,1);
	$result = $main->update_data($data, $where);
	$return_page = $cf_module_path.'detail-act_sp.htm?id='.$id;
	if ($result) {
		my_reload_top();
	   // my_jump('操作完成', '', $return_page, 1);
	} else {
	    my_jump('操作失败');
	}
	break;
	
	case "status":
	$value = intval($_GET['value']);
	$reason = trim($_GET['value2']);
	$where = "sp_id = '" . $id . "'";
	$data = array(
	    'sp_status' => intval($value),
	    'sp_fail_reason' => $reason,
	);
	//$db->debug(1,1);
	$result = $main->update_data($data, $where);
	$return_page = $cf_module_path.'detail-act_sp.htm?id='.$id;
	if ($result) {
	    my_jump('操作完成', '', $return_page, 1);
	} else {
	    my_jump('操作失败');
	}
	break;

	

    case "all":
	$alltype = $_POST['alltype'];
	$idlist = $_POST['idlist'];
	if ($idlist) {
	    $list = implode(",", $idlist);
	    switch ($alltype) {
		/*case "alldel":
		    $result = $main->del_data("id in (" . $list . ")");
		    break;*/
		default:
		    my_jump('請指定操作方式');
		    break;
	    }
	    if ($result)
		my_jump('操作成功', '', $return_page, 1);
	    else
		my_jump('操作失敗');
	}else {
	    my_jump('输入的参数有误');
	}
	break;
    default:
	my_jump('非法操作!');
	break;
}

$smarty->assign(array(
    'id' => $id,
    'row' => htmlchar($row),
    'data_ary' => $data_ary,
	'type_ary' => $type_ary,
    'sessionid' => session_id(),
    'page' => $page,
    'self' => $self,
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
    'skey4' => $skey4,
    'skey5' => $skey5,
    'skey6' => $skey6,
	'skey7' => $skey7,
	'skey8' => $skey8,
	'skey9' => $skey9,
    'stype' => $stype,
	'stype_ary' => $stype_ary,
    'stype2' => $stype2,
	'key_word' => $key_word,
    'group' => $group,
    'result' => $result,
    'sum' => $sum,
    'club' => add_array_item(array('' => '==請選擇=='), $cf_info_var_common['club_type_act'])
));


$smarty->mydisplay();
?>